package urjc.as.emergencies.client.testing;

import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;

import urjc.as.emergencies.client.CommToolkit;
import urjc.as.emergencies.data.Ambulance;
import urjc.as.emergencies.data.PatientRecord;
import urjc.as.emergencies.data.Mission;
import urjc.as.emergencies.data.PatientInfo;


public class TestClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//Creamos la misión
		System.out.println(">>>Creamos una misión");
		String missionUri = createMission();
		
		//Rellenamos la información del paciente
		System.out.println(">>>Rellenamos la información del paciente");
		Form form = new Form();
		form.add(PatientInfo.NAME, "Perico el de los palotes");
		form.add(PatientInfo.DIAGNOSIS, "To chungo");
		System.out.println(sendPatch(missionUri+"/patientInfo", form));
		
		//Rellenamos la información del historial
		System.out.println(">>>Completamos el historial del paciente");
		form = new Form();
		form.add(PatientRecord.ALLERGIES, "Pelo de zorra");
		form.add(PatientRecord.DISEASES, "Dolor de cabeza");
		form.add(PatientRecord.MEDICINES, "Paracetamol y agua");
		System.out.println(sendPatch(missionUri+"/patientInfo/historial", form));
		
		
		
		//Cambiamos el estado de la misión a uno incoherente
		System.out.println(">>>Cambio de estado a uno NO coherente");
		form = new Form();
		form.add(Mission.STATUS, Mission.EN_HOSPITAL+"");
		System.out.println(sendPatch(missionUri, form));
		
		//Creamos una ambulancia
		System.out.println(">>>Creamos 4 nuevas ambulancia");
		Ambulance ambulance = new Ambulance();
		ambulance.setStatus(Ambulance.RECARGANDO);
		System.out.println(CommToolkit.createAmbulance(ambulance, "http://localhost:8080/ambulancias"));
		System.out.println(CommToolkit.createAmbulance(ambulance, "http://localhost:8080/ambulancias"));
		System.out.println(CommToolkit.createAmbulance(ambulance, "http://localhost:8080/ambulancias"));
		ambulance.setStatus(Ambulance.LIBRE);
		System.out.println(CommToolkit.createAmbulance(ambulance, "http://localhost:8080/ambulancias"));
		
		CommToolkit.getRepresentation("http://localhost:8080/ambulancias");
////		
////		//Recibimos la lista de ambulancias libres
////		
////		
		//Asignamos una ambulancia a la misión
		System.out.println(">>>Asignamos una ambulancia");
		form = new Form();
		form.add(Mission.AMBULANCE, "http://localhost:8080/ambulancias/345");
		System.out.println(sendPatch(missionUri, form));
//		
		//Asignamos una ambulancia a la misión
//		System.out.println(">>>Asignamos una ambulancia");
//		form = new Form();
//		form.add(Mission.AMBULANCE, "http://localhost:8080/ambulancias/345");
//		System.out.println(sendPatch(missionUri, form));
		
//		
//		desarrollaMission(missionUri);
////		System.out.println(">>>Aborta misión");
////		form = new Form();
////		form.add(Mission.STATUS, Mission.ABORT+"");
////		System.out.println(sendPatch(missionUri, form));
	}
	
	public static String createMission() {
		Mission mission = new Mission();
		
		String xml = mission.serializar();
		Request request = new Request(Method.POST, CommToolkit.MISSIONS);
		request.setEntity(xml, MediaType.TEXT_XML);
		
		// Realizamos la petición
		Response resp = CommToolkit.doRequest(request);
		return resp.getLocationRef().toString();
	}

	
	public static Status sendPatch(String _uri, Form _form) {
		Request request = new Request(new Method("PATCH"), _uri);
		request.setEntity(_form.getWebRepresentation());
		Response resp = CommToolkit.doRequest(request);
		return resp.getStatus();
	}
	
	public static void desarrollaMission(String missionUri) {
		//Desarrollamos la misisón
		System.out.println(">>>Esperando ambulancia");
		Form form = new Form();
		form.add(Mission.STATUS, Mission.ESPERANDO_AMBULANCIA+"");
		System.out.println(sendPatch(missionUri, form));
		
		System.out.println(">>>Siendo atendido");
		form = new Form();
		form.add(Mission.STATUS, Mission.SIENDO_ATENDIDO+"");
		System.out.println(sendPatch(missionUri, form));
		
		System.out.println(">>>Trasladamos al hospital");
		form = new Form();
		form.add(Mission.STATUS, Mission.CAMINO_HOSPITAL+"");
		System.out.println(sendPatch(missionUri, form));
		
		System.out.println(">>>En el hospital");
		form = new Form();
		form.add(Mission.STATUS, Mission.EN_HOSPITAL+"");
		System.out.println(sendPatch(missionUri, form));
		
		System.out.println(">>>Cerramos la misisión");
		form = new Form();
		form.add(Mission.STATUS, Mission.CLOSE+"");
		System.out.println(sendPatch(missionUri, form));
	}
}
